﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Discio
{
  
    

    public class Source  
    {
        private static readonly string dataFolderSetting = "Discio.DataFolder";
        private static readonly string shadowFolderSetting = "Discio.ShadowFolder";
        private static readonly string extensionSetting = "Discio.Extension";

        /// <summary>
        /// Encrypt a string using the local AES encryption class
        /// </summary>
        /// <param name="text">Text</param>
        /// <returns>Encrypted text</returns>
        public static string Encrypt(string text)
        {
            AES enc = new AES();
            return enc.EncryptToString(text);
        }

        public bool ConfigSetup()
        {
            if (ConfigurationManager.AppSettings[dataFolderSetting] != null)
            {
                DataFolder = ConfigurationManager.AppSettings[dataFolderSetting].ToString();

              
                if (ConfigurationManager.AppSettings[extensionSetting] != null)
                {
                    Extension = ConfigurationManager.AppSettings[extensionSetting].ToString();
                }

                return true;

            }

            return false;

        }

        public string Extension = "json";
        public int MaxJsonLength = 10485760; //10MB

        

        public string DataFolder { set; get; }
        

        public bool IsArchived(string name)
        { 
           string folder = DataFolder.TrimEnd('/').TrimEnd('\\');
           string path = string.Concat(folder,@"\",name,@"\archive.json");

           return File.Exists(path);  
        }

        public void Archive(string name, int records)
        {
            if (!IsArchived(name))
            {

            }
        }

        public string ArchivePath(string name)
        {
            if (DataFolder != null)
            {
                string folder = DataFolder.TrimEnd('/').TrimEnd('\\');
                return string.Concat(folder, @"\", name);
            }
            return "";
        }

        public string StoragePath(string name)
        {
            if (DataFolder != null)
            {
                string folder = DataFolder.TrimEnd('/').TrimEnd('\\');
                return string.Concat(folder,@"\",name); 
            }
            return "";
        }

      

        public string MasterPath(string name)
        {
            return string.Concat(StoragePath(name),@"\master.",Extension);
        }

        public string SubsetPath(string name,string subset)
        {
            return string.Concat(StoragePath(name),@"\",subset,Extension);
        }
 

        public bool IsValid(string name)
        {
            if (DataFolder == null)
            {
                return false;
            }

            string folder = StoragePath(name);
            var master =  string.Concat(folder,@"\master.",Extension); 
       
            return File.Exists(master);

        }

        public ValidationResult Validate(string name)
        {
            string folder = StoragePath(name);
            var master = string.Concat(folder,@"\master.",Extension);
        

            ValidationResult result = new ValidationResult{ Folder=false, Master=false };

            result.Folder = Directory.Exists(folder);
            result.Master = File.Exists(master);
 

            return result;
        }


        public void Split(string name, int partition)
        {
            if (Validate(name).Master)
            {

            }
        }

        public bool Reset(string name)
        {
            string folder = StoragePath(name);

            var master = string.Concat(folder,@"\master.",Extension); 

            DirectoryInfo dir = new DirectoryInfo(folder);

            if (!dir.Exists)
            {
                return false;
            }

            Empty(dir);
             
            File.Create(master); 

            return true;
        }

        private bool CreateSet(string name, bool overwrite)
        {
            string folder =  StoragePath(name);

            var master = string.Concat(folder,@"\master.",Extension);

            DirectoryInfo dir = new DirectoryInfo(folder);

            if (dir.Exists && !overwrite)
            {
                return false;
            }
            else if (dir.Exists && overwrite)
            {
                Empty(dir);
            }
            else if (!dir.Exists)
            {
                dir.Create();
            }

            File.Create(master).Close();

            return true;
        }

        public CreationResult Create(string name)
        {
            return Create(name, false);
        }

        public CreationResult Create(string name, bool overwrite)
        {
            CreationResult result = new CreationResult
            {
                Master = CreationState.NotRequested
            };

            result.Master = CreateSet(name, overwrite)? CreationState.Created:CreationState.Failed;

            return result;
        }

 
        private void Empty(DirectoryInfo dir){

            foreach (FileInfo file in dir.GetFiles()) { 
                file.Delete(); 
            }
                
            foreach(DirectoryInfo sub in dir.GetDirectories()){
                sub.Delete(true);
            };
    
        }


    }

    public struct ValidationResult
    {
        public bool Folder { get; set; }
        public bool Master { get; set; }
    }

    public struct CreationResult
    {
        public CreationState Master { get; set; }
    }

    public enum CreationState
    { 
        Failed = 0,
        Created = 1,
        NotRequested = 2
    }



}
